package com.gmall.console.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmall.console.base.Result;
import com.gmall.console.common.DataCons;
import com.gmall.console.system.entity.Shoping;
import com.gmall.console.system.entity.User;
import com.gmall.console.system.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jone
 * @since 2024-05-28
 */
@RestController
@Tag(name = "用户管理", description = "用户 API")
@RequestMapping("/user")
@CrossOrigin //跨域处理
public class UserController {

    @Resource
    IUserService iUserService;
    @Resource
    PasswordEncoder passwordEncoder;

    @PostMapping("/login")
    @Operation(summary = "登录接口", description = "登录接口描述")
    public Result<Map<String,Object>> login(@RequestBody User user){

        Map<String,Object> map = iUserService.login(user);
        if(null == map){
            return Result.fail(DataCons.MSG_CODE_201,"登录失败，用户名或密码错误！");
        }

        return Result.success(map,"登录成功");
    }

    @PostMapping("/add")
    @Operation(summary = "新增用户接口", description = "新增接口描述")
    public Result<Map<String,Object>> add(@RequestBody User user){
        //密码加密处理 60位，做了加盐处理，每次同样密码，加密结果不一样
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setCreateBy(user.getUserName());
        user.setCreateTime(new Date());
        iUserService.addUser(user);
        return Result.success("数据添加成功");
    }

    @PutMapping("/update")
    @Operation(summary = "修改用户接口", description = "修改接口描述")
    public Result<Map<String,Object>> update(@RequestBody User user){
        user.setPassword(null);
        user.setUpdateTime(new Date());
        iUserService.updateUserById(user);
        return Result.success("修改用户成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取用户信息", description = "获取用户信息")
    public Result<User> getUserInfo(@PathVariable("id") String id){
        User user = iUserService.getUserById(id);
        return Result.success(user);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户信息", description = "删除用户信息")
    public Result<User> deleteUser(@PathVariable("id") String id){
        iUserService.removeUserById(id);
        return Result.success("删除用户成功！");
    }

    @GetMapping("/list")
    @Operation(summary = "用户列表接口", description = "用户列表接口描述")
    public Result<Map<String,Object>> list(@RequestParam(value = "userName",required = false) String userName,
                                   @RequestParam(value = "iphone",required = false) String iphone,
                                   @RequestParam("pageNo") int pageNo,
                                   @RequestParam("pageSize") int pageSize) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(userName),User::getUserName,userName);
        wrapper.eq(StringUtils.isNotEmpty(iphone),User::getIphone,iphone);
        wrapper.orderByDesc(User::getId);

        Page<User> page = new Page(pageNo,pageSize);

        iUserService.page(page, wrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data,"查询成功");
    }

    @GetMapping("/info")
    @Operation(summary = "用户信息接口", description = "用户信息接口描述")
    public Result<Map<String,Object>> info(@RequestParam("token") String token) {
        Map<String,Object> data = iUserService.getUserInfo(token);
        if(null == data){
            return Result.fail(DataCons.MSG_CODE_201,"登录已经失效，请重新登录");
        }

        return Result.success(data,"查询成功");
    }

    @PostMapping("logout")
    public Result<?> logout(@RequestHeader("X-Token") String token){
        //从请求头获取token
        iUserService.logout(token);
        return Result.success();
    }



}
